/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-04-28 11:24:35
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2024-07-30 14:23:08
 * @FilePath: \greenbeeadmin\controllers\admin\admin.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package controllers

import (
	"encoding/json"
	adminService "greenbeeadmin/controllers/admin/service"
	greenbee "greenbeeadmin/controllers/greenbee"
	greenbeeUtils "greenbeeadmin/controllers/greenbee/utils"
	"greenbeeadmin/models"
	"strconv"
)

// 邮箱设置 controller
type EmailSetController struct {
	greenbee.AdminBaseController
}

func (c *EmailSetController) Get() {
	var systemSetService adminService.SystemSetService
	var systemSetting models.SystemSetting
	systemSetting.Types = "email"
	field := []string{"Types"}
	data, _ := systemSetService.GetData(systemSetting, field)
	c.Data["systemSetting"] = data

	//解析邮箱配置参数
	var email_data greenbee.EmailSystemData
	if len(data.Gonggao) > 0 {
		json.Unmarshal([]byte(data.Gonggao), &email_data)
	}

	c.Data["emailSetting"] = email_data

	c.TplName = "admin/system/email_setting.html"
}

// 提交数据
func (c *EmailSetController) Post() {
	var data interface{}
	id, _ := c.GetInt("id", 0)
	server_host := c.GetString("server_host", "")
	server_port := c.GetString("server_port", "")
	from_email := c.GetString("from_email", "")
	from_passwd := c.GetString("from_passwd", "")

	var systemSetService adminService.SystemSetService
	var systemSetting models.SystemSetting
	systemSetting.Types = "email"
	field := []string{"Types"}
	_, err := systemSetService.GetData(systemSetting, field)

	//组装邮箱配置信息
	var email_data greenbee.EmailSystemData
	email_data.From_email = from_email
	email_data.Server_host = server_host
	email_data.From_passwd = from_passwd
	email_data.Server_port = server_port

	b, errors := json.Marshal(email_data)
	if errors != nil {
		c.ErrorJson(500, "参数解析失败", 0, data)
	}
	json_str_val := string(b)
	systemSetting.Gonggao = json_str_val

	if err == nil || id > 0 {
		//更新
		updateField := []string{"Gonggao"}
		systemSetService.Edit(systemSetting, id, updateField)
	} else {
		//添加
		systemSetService.Add(systemSetting)
	}
	c.SuccessJson(0, data, 1)
}

// 邮箱测试 controller
type EmailTestController struct {
	greenbee.AdminBaseController
}

func (c *EmailTestController) Get() {
	c.TplName = "admin/system/email_test.html"
}

// 测试邮件 提交数据
func (c *EmailTestController) Post() {
	var data interface{}
	email_addr := c.GetString("email_addr", "")
	email_title := c.GetString("email_title", "")
	email_content := c.GetString("email_content", "")

	if email_addr == "" || email_title == "" || email_content == "" {
		c.ErrorJson(500, "参数有误", 0, data)
	}
	to := []string{email_addr}
	//查询邮箱配置信息
	email_data, err := c.GetSystemEmailSetingData()
	if err != nil {
		c.ErrorJson(500, err.Error(), 0, data)
	}
	if email_data.From_email == "" || email_data.From_passwd == "" || email_data.Server_host == "" || email_data.Server_port == "" {
		c.ErrorJson(500, "邮箱参数不全，请先配置邮箱参数", 0, data)
	}
	serverHost := email_data.Server_host
	fromEmail := email_data.From_email
	fromPasswd := email_data.From_passwd
	serverPort, _ := strconv.Atoi(email_data.Server_port)
	res := greenbeeUtils.GomailSendToMail(to, email_title, email_content, serverHost, fromEmail, fromPasswd, serverPort)
	if res == "ok" {
		c.SuccessJson(0, data, 1)
	}
	c.ErrorJson(500, res, 0, data)
}
